Skip to content

fix(s02): handle Anthropic SDK content blocks in normalize_messages#207

Open
wadeKeith wants to merge 3 commits into
shareAI-lab:mainfrom
wadeKeith:fix/s02-tool-use-normalize-messages
Open

fix(s02): handle Anthropic SDK content blocks in normalize_messages#207
wadeKeith wants to merge 3 commits into
shareAI-lab:mainfrom
wadeKeith:fix/s02-tool-use-normalize-messages

Conversation

@wadeKeith
Copy link
Copy Markdown

Problem

s02_tool_use.py crashes with:

anthropic.BadRequestError: 400 - tool result's tool_use_id not found

Root Cause

normalize_messages() uses isinstance(block, dict) to filter content blocks, but the Anthropic SDK stores response.content as pydantic model objects (ToolUseBlock, TextBlock), not plain dicts. This silently drops all tool_use blocks during normalization, so the subsequent tool_result references an id the API never saw.

Fix

Added a _block_to_dict() helper that converts SDK objects to plain dicts via model_dump() (with vars() fallback), and removed the isinstance(block, dict) guard so all content blocks are properly serialized.

Changes

  • agents/s02_tool_use.py: +13 / -3 lines
    • New _block_to_dict(block) function
    • Updated list comprehension in normalize_messages() to use it

Tested locally — the agent loop now completes tool calls without errors.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 10, 2026

@wadeKeith is attempting to deploy a commit to the crazyboym's projects Team on Vercel.

A member of the Team first needs to authorize it.

@wadeKeith wadeKeith mentioned this pull request Apr 10, 2026
@CrazyBoyM CrazyBoyM force-pushed the main branch 2 times, most recently from 36897b1 to d882d01 Compare April 14, 2026 16:11
@wadeKeith wadeKeith force-pushed the fix/s02-tool-use-normalize-messages branch from faca156 to bab0e67 Compare May 9, 2026 11:22
@wadeKeith
Copy link
Copy Markdown
Author

The Vercel deploy CI failures appear to be an organization-level permission issue — the deploy is attempting to target the crazyboym's projects team on Vercel, which requires authorization. This is not a code issue and needs to be resolved by a repo maintainer with Vercel access.

@wadeKeith
Copy link
Copy Markdown
Author

Quick update on the CI situation:

  1. Code fix is correct — ran pytest tests/test_agents_smoke.py locally, all 21 tests pass. The _block_to_dict() helper now properly handles Anthropic SDK content block objects (with model_dump() / vars() fallback).

  2. Vercel deployment failureAuthorization required to deploy. This is expected for fork PRs. Vercel requires the upstream repo owner to authorize external contributors before deployments from fork branches can proceed. The GitHub Actions CI (ci.yml + test.yml) runs are in action_required state likely because they're waiting on a first-time contributor approval.

  3. Action needed from maintainers: Approve the workflow run (if first-time contributor approval is required) and/or authorize Vercel deployments from fork PRs in the Vercel project settings.

The actual Python + web build code changes are clean and tested.

CrazyBoyM and others added 3 commits May 12, 2026 15:52
The original normalize_messages() used isinstance(block, dict) to filter
content blocks, but Anthropic SDK returns pydantic model objects (e.g.
ToolUseBlock, TextBlock), not plain dicts. This caused tool_use blocks
to be silently dropped, leading to 'tool_result tool_use_id not found'
errors (400) on the next API call.

Added _block_to_dict() helper that converts SDK objects via model_dump()
or vars() fallback, and removed the isinstance(block, dict) guard so
all content blocks are properly serialized.
@wadeKeith wadeKeith force-pushed the fix/s02-tool-use-normalize-messages branch from bab0e67 to 3774147 Compare May 12, 2026 07:53
@wadeKeith
Copy link
Copy Markdown
Author

Rebased to resolve merge conflicts. The Vercel deployment failures are expected for fork PRs - not a code issue. Ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants